﻿using JobBatDeal.Common.EsTool;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JobBatDeal.Common
{
    public class MergeSimilarityGenerator
    {
        private const double HotelNameSimilarityTolerance = 0.85D;
        /// <summary>
        /// 200 米内
        /// </summary>
        private const double HotelCoordinateSimilarityTolerance = 0.9D;

        private double GetCoordinateSimilarity(
            decimal? supplierLatitude,
            decimal? supplierLongitude,
            decimal? fansCarLatitude,
            decimal? fansCarLongitude
        )
        {
            var coordinate = new GeoCoordinate(
                (double)(supplierLatitude ?? 0M),
                (double)(supplierLongitude ?? 0M));

            var fansCarCoordinate = new GeoCoordinate(
                (double)(fansCarLatitude ?? 0M),
                (double)(fansCarLongitude ?? 0M));


            var hotelCoordinateSimilarity = coordinate.GetCoordinateSimilarity(fansCarCoordinate);

            return hotelCoordinateSimilarity;
        }

        private double GetNameSimilarity(string supplierHotelName, string hotelName)
        {
            // 酒店名称相似度
            var hotelNameSimilarity = supplierHotelName.GetJaroWinklerSimilarity(hotelName);

            if (hotelNameSimilarity < HotelNameSimilarityTolerance)
            {
                return hotelNameSimilarity;
            }

            var brandKeyList = new List<string>();// _hotelSimilarityOption.BrandKeyList;
            if (brandKeyList == null || !brandKeyList.Any())
            {
                return hotelNameSimilarity;
            }

            var hotelNameFs = new List<int>();
            var hotelNameSupplierFs = new List<int>();

            for (var i = 0; i < brandKeyList.Count; i++) //轮询品牌关键字集合，判断酒店名称中是否包含关键字
            {
                if (hotelName.Contains(brandKeyList[i]))
                {
                    hotelNameFs.Add(i);
                }
                if (supplierHotelName.Contains(brandKeyList[i]))
                {
                    hotelNameSupplierFs.Add(i);
                }
            }

            if (hotelNameFs.Count == hotelNameSupplierFs.Count)
            {
                if (hotelNameFs.Count == 0)
                {
                    return hotelNameSimilarity;
                }

                //酒店名称和供应商酒店名称所含关键词数量相同
                var cCount = 0;
                for (var i = 0; i < hotelNameFs.Count; i++)
                {
                    if (hotelNameFs[i] == hotelNameSupplierFs[i])
                    {
                        cCount++;
                    }
                    else
                    {
                        break;
                    }
                }

                //酒店名称和供应商酒店名称所含关键词都相同，则验证通过
                if (cCount > 0 && cCount == hotelNameFs.Count)
                {
                    return hotelNameSimilarity;
                }

                hotelNameSimilarity = 0;//验证不通过，则相似度为0
            }
            else
            {
                hotelNameSimilarity = 0;//如果包含关键字的数量不同，相似度为0
            }

            return hotelNameSimilarity;
        }

        private static decimal GetPhoneSimilarity(string supplierHotelPhone, string hotelPhone)
        {
            var hotelPhoneSimilarity = string.IsNullOrWhiteSpace(supplierHotelPhone) ||
                                       supplierHotelPhone.StartsWith(@"\n") ||
                                       supplierHotelPhone.StartsWith("400") ||
                                       supplierHotelPhone.ToCleanStr() != hotelPhone.ToCleanStr() ? 0 : 1;

            return hotelPhoneSimilarity;
        }

        public static decimal GetPhoneSimilarityRmPrex(string supplierHotelPhone, string hotelPhone)
        {
            if (string.IsNullOrEmpty(supplierHotelPhone) || string.IsNullOrEmpty(hotelPhone))
            {
                return 0;
            }

            supplierHotelPhone = supplierHotelPhone.Replace("+86-", "0");

            if (supplierHotelPhone.IndexOf("0086-", StringComparison.Ordinal) == 0)
            {
                supplierHotelPhone = supplierHotelPhone.Replace("0086-", "");
            }
            if (supplierHotelPhone.IndexOf("86-", StringComparison.Ordinal) == 0)
            {
                supplierHotelPhone = supplierHotelPhone.Replace("86-", "");
            }

            hotelPhone = hotelPhone.Replace("+86-", "0");
            if (hotelPhone.IndexOf("0086-", StringComparison.Ordinal) == 0)
            {
                hotelPhone = hotelPhone.Replace("0086-", "");
            }
            if (hotelPhone.IndexOf("86-", StringComparison.Ordinal) == 0)
            {
                hotelPhone = hotelPhone.Replace("86-", "");
            }

            //过滤最后两位分级号-0
            supplierHotelPhone = RemoveEndNum(supplierHotelPhone);
            hotelPhone = RemoveEndNum(hotelPhone);

            if (hotelPhone.Length > 10 && supplierHotelPhone.Length > 10 && (hotelPhone.Contains(supplierHotelPhone) || supplierHotelPhone.Contains(hotelPhone)))
            {
                return 1;
            }

            var hotelPhoneSimilarity = string.IsNullOrWhiteSpace(supplierHotelPhone) ||
                                       supplierHotelPhone.StartsWith(@"\n") ||
                                       supplierHotelPhone.StartsWith("400") ||
                                       supplierHotelPhone.ToCleanStr() != hotelPhone.ToCleanStr() ? 0 : 1;

            return hotelPhoneSimilarity;
        }

        private static string RemoveEndNum(string phoneStr)
        {
            if (phoneStr.Length > 7)
            {
                var i = phoneStr.LastIndexOf('-');
                if (i > 0 && i == phoneStr.Length - 2)
                {
                    phoneStr = phoneStr.Substring(0, i);
                }
            }
            return phoneStr;
        }

    }
}
